package com.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * @author Yancy
 * @program springioctest
 * @create 2021-01-18
 */
public class LoginThread extends Thread {

  private Socket socket;

  public LoginThread(Socket socket) {
    this.socket = socket;
  }

  @Override
  public void run() {
    DataInputStream dis = null;
    DataOutputStream dos = null;
    try {
      // 接收来自客户端的数据并输出
      dis = new DataInputStream(socket.getInputStream());
      String result = dis.readUTF();
      System.out.println(result);

      // 发送反馈数据给客户端
      dos = new DataOutputStream(socket.getOutputStream());
      dos.writeUTF("登录成功");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (dos != null){
            dos.close();
        }

      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        if (dis != null){
            dis.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
        try {
            if(socket!=null){
                socket.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


  }
}
